package com.rcm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rcm.dao.AdmininfoMapper;
import com.rcm.dao.CoursesinfoMapper;
import com.rcm.dao.StudentinfoMapper;
import com.rcm.dao.TeacherinfoMapper;
import com.rcm.model.Admininfo;
import com.rcm.model.Coursesinfo;
import com.rcm.model.Studentinfo;
import com.rcm.model.Teacherinfo;
import com.rcm.utility.MyBatisTool;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentinfoMapper mapper = null;
	private CoursesinfoMapper mapper2 = null;
	private AdmininfoMapper mapper3 = null;
	private TeacherinfoMapper mapper4 = null;

	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		SqlSession session = MyBatisTool.getSession();
		mapper = session.getMapper(StudentinfoMapper.class);
		CoursesinfoMapper mapper2 = session.getMapper(CoursesinfoMapper.class);
		AdmininfoMapper mapper3 = session.getMapper(AdmininfoMapper.class);
		TeacherinfoMapper mapper4 = session.getMapper(TeacherinfoMapper.class);
		response.setCharacterEncoding("uft-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String sid = request.getParameter("sid");
		String password = request.getParameter("password");
		List<Studentinfo> list = mapper.selectAll();
		List<Coursesinfo> list2 = mapper2.selectAll();
		List<Admininfo> list3 = mapper3.selectAll();
		PageHelper.startPage(1,10);
		List<Teacherinfo> list4 = mapper4.selectAll();
		System.out.println(sid);
		System.out.println(password);
		session.close();
		// 判断学生账号密码是否输入正确
		for (Studentinfo c : list) {
			if ((sid.equals(c.getEmail()) || sid.equals(c.getSid())) && password.equals(c.getPassword())) {
				request.getSession().setAttribute("c", c);
				request.getSession().setAttribute("name", c.getName());
				System.out.println("学生登录");
				// 如果账号密码输入正确，则判断是否有选课记录，
				// 如果有选课记录则 跳转到选课的结果页面
				for (Coursesinfo c2 : list2) {
					if (c.getSid().equals(c2.getSid())) {
						request.getSession().setAttribute("c2", c2);
						request.getSession().setAttribute("name", c.getName());
						request.getSession().setAttribute("coursess", "true");
						request.getRequestDispatcher("courses.jsp").forward(request, response);
						return;
					}
				}
				// 如果没有选课记录则跳转到登录首页
				request.getRequestDispatcher("studentIndex.jsp").forward(request, response);
				System.out.println("选课");
			}
		}
		// 判断管理员账号密码是否输入正确
		for (Admininfo c3 : list3) {
			if (c3.getUsername().equals(sid) && (c3.getPassword().equals(password))) {
				System.out.println("管理员登录");
				request.getSession().setAttribute("c3", c3);
				request.getSession().setAttribute("adminname", c3.getUsername());
				int pageNum=1;
				if (request.getParameter("pageNum")!=null) {
					pageNum=Integer.parseInt(request.getParameter("pageNum"));
				}

				
				session.close();
				PageInfo<Teacherinfo> pageInfo=new  PageInfo<Teacherinfo>(list4);
				System.out.println(list4);
				request.setAttribute("pageInfo", pageInfo);
				request.getRequestDispatcher("/admininfo.jsp").forward(request, response);
				

				
				return;
			}
		}

		// 判断教师账号密码是否输入正确
		for (Teacherinfo c4 : list4) {
			if (c4.getTid().equals(sid) && (c4.getTpassword().equals(password))) {
				System.out.println("教师登录");
				request.getSession().setAttribute("c4", c4);
		
				request.getSession().setAttribute("Teachername", c4.getTname());
					System.out.println(request.getSession().getAttribute("name"));
					
					
					
				 request.getRequestDispatcher("teacherIndex.jsp").forward(request, response);
				
				return;
			}
		}

		out.print("<script language='javascript'>alert('账号或密码错误');window.location.href='index.jsp';</script>");
		return;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
